package com.kgc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.mapper.ReturnMapper;
import com.kgc.service.IReturnProviderService;
import entity.ResponseMessage;
import entity.StatusCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.order.pojo.ReturnPojo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 *
 * @description:  退货订单service实现类
 *
 * @author: Cheng
 *
 * @create: 2022-04-21 15:20
 **/
@Service
public class IReturnProviderServiceImpl implements IReturnProviderService {

    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private ReturnMapper returnMapper;


    /**
     * 按条件查询退货订单信息
     * @param orderNum
     * @param sname
     * @param startTime
     * @param endTime
     * @param pageNum
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage selectReturnInfo(String orderNum, String sname, Date startTime, Date endTime, int status, int pageNum) {
        logger.info("IReturnProviderServiceImpl selectReturnInfo is start ..... params:olderNum" + orderNum + ",sname:->" + sname + ",startTime:->" + startTime + ",endTime:->" + endTime + ",status" + status + ",pageNum" + pageNum);
        ResponseMessage responseMessage = new ResponseMessage();
        HashMap<String,Object> map = new HashMap<>();
        try {
            map.put("orderNum", orderNum);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("type", status);
            PageHelper pageHelper = new PageHelper();
            pageHelper.startPage(pageNum, 5);
            List<ReturnPojo> returnList= returnMapper.selectReturnInfo(map);
            PageInfo<ReturnPojo> pageInfo = new PageInfo<>(returnList);
            logger.debug("IReturnProviderServiceImpl selectReturnInfo result is pageInfo" + pageInfo);
            if (pageInfo != null) {
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setMessage("退货订单查询成功!");
                responseMessage.setData(pageInfo);
                responseMessage.setFlag(true);
            } else {
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("查询退货订单时出现了错误");
                return responseMessage;
            } 
        } catch (Exception e) {
            logger.error("IReturnProviderServiceImpl selectReturnInfo is error", e);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("IReturnProviderServiceImpl selectReturnInfo出现了错误");
            e.printStackTrace();
        }
        return responseMessage;
    }
    
}
